package com.example.administrator.szb.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.PopupWindow;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.example.administrator.szb.activity.LoginNewActivity_two;
import com.example.administrator.szb.activity.layout.RZActivity;
import com.example.administrator.szb.activity.layout.RZCenterActivity2;
import com.example.administrator.szb.bean.UserInfo2;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.HttpUtils;
import com.example.administrator.szb.http.OnResultListener;
import com.example.administrator.szb.http.URLAddress;

import java.util.HashMap;
import java.util.Map;

import static com.umeng.socialize.utils.DeviceConfig.context;

/**
 * Created by stone on 2018/1/22.
 */

public class CheckInfoUtils {

    //登录，认证，身份证号是否已正确提交（接口未给，暂不验证），
    public static boolean check(Activity context, IosDiaolog iosDiaolog, View view,OnSuccessLietener lietener){
        if (!isLogin(context)||!isSeniorCertified( context,  iosDiaolog,  view,lietener)||!idCardRight(iosDiaolog,  view)){
            return false;
        }
        return true;
    }
   public interface OnSuccessLietener{
        void onSuccess();
    }

    public static boolean isLogin(Context context){
        if (!SPUtils.isLogin()){
            Intent intent = new Intent(context, LoginNewActivity_two.class);
            context.startActivity(intent);
            return false;
        }
        return true;
    }
    public static boolean isSeniorCertified(final Activity activity,final IosDiaolog iosDiaolog,final View view,final OnSuccessLietener lietener){
        requestUserInfo(activity,new OnStatusListener() {
            @Override
            public void onStatus(int status) {
                if (status!=3){
                    String tips = "您的账号尚未认证，请先认证";
                    switch (status){
                        case 1:
                            tips = "您的账号尚未认证，请先认证";
                            break;
                        case 2:
                            tips = "您的账号正在审核中,不能发布任务";
                            break;
                        case 4:
                            tips = "您的账号审核未通过,不能发布任务";
                            break;
                    }

                    //您的账号尚未认证，请先认证，/您的账号正在审核中,不能发布任务/您的账号审核未通过,不能发布任务/
                    iosDiaolog.reset();
                    iosDiaolog.setMsgs(tips);
                    iosDiaolog.setLeftButton("再看看");
                    iosDiaolog.setRightButton("立即认证");
                    iosDiaolog.setRightClick(new IosDiaolog.OnIosDialogClickListener() {
                        @Override
                        public void onClick(String str, PopupWindow popupWindows) {
                            Class c = RZCenterActivity2.class;
                            if (SPUtils.getUserinfo().getType()==1){
                                c = RZActivity.class;
                            }
                            Intent intent = new Intent(activity, c);
                            intent.putExtra("rzStatu",SPUtils.getUserinfo().getStatus());

                            activity.startActivity(intent);
                        }
                    });
                    iosDiaolog.alert(view);



                }else {
                    if (lietener!=null)
                    lietener.onSuccess();
                }
            }

            @Override
            public void onFail() {
                Toasts.show(context,"网络错误");
            }
        });
        return true;
    }




    public static boolean idCardRight(final IosDiaolog iosDiaolog,View view){
        boolean flag = true;
        if (!flag){
            iosDiaolog.reset();
            iosDiaolog.setMsgs("请输入身份证号校验");
            iosDiaolog.setAtuoHideRight(false);
            iosDiaolog.setRightClick(new IosDiaolog.OnIosDialogClickListener() {
                @Override
                public void onClick(String str, PopupWindow popupWindows) {
                    if (TextUtils.isEmpty(str)){
                        iosDiaolog.setErrorMsg("您输入的身份证号码有误");
                        return;
                    }
                    iosDiaolog.setErrorMsg("");
                    popupWindows.dismiss();
                }
            });
            iosDiaolog.confirm(view);
        }
        return true;
    }
    private static void requestUserInfo(Activity activity,final OnStatusListener listener){
        Map<String,Object> params = new HashMap<>();
        params.put("code", HttpUtil.getParam());
        params.put("id", SPUtils.getUserId());
        HttpUtils.post(activity,URLAddress.URLBASE + URLAddress.URL_CENTER, params, new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {
                UserInfo2 userInfo2 = JSON.parseObject(str,new TypeReference<UserInfo2>(){});
                if (listener!=null){
                    listener.onStatus(userInfo2.getStatus());
                }

            }

            @Override
            public void onFailure(int requestCode, String str) {
                if (listener!=null)
                    listener.onFail();
            }
        });
    }
    public interface OnStatusListener{
        void onStatus(int status);
        void onFail();
    }
}
